package main

import "fmt"

/*
* Author: JacksonCoder
* CreateTime: 2024/11/30
 */

func Closure(name string) func() string {
	// 闭包
	return func() string {
		return "hello " + name
	}
}

func Closure1() func() string {
	// 闭包
	name := "alex"
	age := 15

	return func() string {
		return fmt.Sprintf("hello, %s  %d", name, age)
	}
}

func Closure2() func() int {
	var age = 0
	fmt.Printf("out: %p ", &age)
	return func() int {
		fmt.Printf("before: %p ", &age)
		age++
		fmt.Printf("after: %p ", &age)
		return age
	}
}

func main() {
	fn := Closure("jackson")
	println(fn())

	fn1 := Closure1()
	println(fn1())

	fun := Closure2()
	println(fun())
	println(fun())
	println(fun())
	println(fun())
	println(fun())

	fun = Closure2()
	println(fun())
	println(fun())
	println(fun())
	println(fun())
	println(fun())
}
